package site.isscloud.jt809;

// CompanyStatisticsInfo.java - 企业信息统计(0x1900)

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class CompanyStatisticsInfo {
    private String companyId;      // 企业ID
    private String companyName;    // 企业名称
    private int totalVehicles;     // 车辆总数
    private int onlineVehicles;    // 在线车辆数
    private int totalDrivers;      // 驾驶员总数
    private float complianceRate;  // 合规率(0-1)
    
    // 构造方法、getter/setter、toByteArray()和fromByteArray()方法
    public CompanyStatisticsInfo(String companyId, String companyName, 
                                 int totalVehicles, int onlineVehicles, 
                                 int totalDrivers, float complianceRate) {
        this.companyId = companyId;
        this.companyName = companyName;
        this.totalVehicles = totalVehicles;
        this.onlineVehicles = onlineVehicles;
        this.totalDrivers = totalDrivers;
        this.complianceRate = complianceRate;
    }
    
    public byte[] toByteArray() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        try {
            dos.write(JT809Utils.stringToBytes(companyId, 11));
            dos.write(JT809Utils.stringToBytes(companyName, 200));
            dos.writeInt(totalVehicles);
            dos.writeInt(onlineVehicles);
            dos.writeInt(totalDrivers);
            dos.writeFloat(complianceRate);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baos.toByteArray();
    }
    
    public static CompanyStatisticsInfo fromByteArray(byte[] bytes) {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        DataInputStream dis = new DataInputStream(bais);
        try {
            String companyId = JT809Utils.bytesToString(dis.readNBytes(11));
            String companyName = JT809Utils.bytesToString(dis.readNBytes(200));
            int totalVehicles = dis.readInt();
            int onlineVehicles = dis.readInt();
            int totalDrivers = dis.readInt();
            float complianceRate = dis.readFloat();
            
            return new CompanyStatisticsInfo(
                companyId, companyName, totalVehicles, 
                onlineVehicles, totalDrivers, complianceRate
            );
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    @Override
    public String toString() {
        return "CompanyStatisticsInfo{" +
                "companyId='" + companyId + '\'' +
                ", companyName='" + companyName + '\'' +
                ", totalVehicles=" + totalVehicles +
                ", onlineVehicles=" + onlineVehicles +
                ", totalDrivers=" + totalDrivers +
                ", complianceRate=" + complianceRate +
                '}';
    }
    
    // Getter和Setter方法
    public String getCompanyId() { return companyId; }
    public String getCompanyName() { return companyName; }
    public int getTotalVehicles() { return totalVehicles; }
    public int getOnlineVehicles() { return onlineVehicles; }
    public int getTotalDrivers() { return totalDrivers; }
    public float getComplianceRate() { return complianceRate; }
}
